/**
 *
 */
package com.landmark;

import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * @author Kiran Kumar Motapothula
 *
 */
public class SKUIDFixedLength extends JPanel{
    
    
    /**
     * @param args This class is used to generate a CSV file with SKU_ID and
     * Manufacturer_SKU_ID using the manufacturer id and options from the input
     * file The file should contain a header with the order mentioned
     *
     * ----These are common columns present for all the input files---
     *
     * Manufacturer,Manufacturer_ID,Product_ID,Product_Name,SKU_ID,Manufacturer_SKU_ID,Cost,
     * PRICE,Weight,Keywords,Category1_Unique,Category1_Name,Category2_Unique,Category2_Name,
     * Category3_Unique,Category3_Name,Category4_Unique,Category4_Name,Category5_Unique,Category5_Name,
     * Category6_Unique,Category6_Name,Short_Description,Long_Description,Thumbnail_Path,
     * Thumbnail_Alt_Text,Large_Image_Path,Large_Image_Alt_Text,Stock,OnWeb,Archive,Season_Start,
     * Season_End,Season_Name,
     *
     * ---These columns differ on catalog to catalog, from which we have to
     * generate the SKU_ID and Manufatcurer_SKU_ID---
     *
     * Bag_Color_Code,Bag_Color_Name,Bag_Color_Swatch,Imprint_Color_Code,
     * Imprint_Color_Name,Imprint_Color_Swatch,Packaging_Code,Packaging_Name,Bottom_Board_Code,
     * Bottom_Board_Name,Bottom_Board_Swatch,Size_Name,Size_Swatch,Width_Code,Width_Name,Width_Swatch
     */
    public void start() throws Exception{
        ReadInputFile();
    }

    private void ReadInputFile() throws Exception{
            // User file path inputs
            String inputFilePath ="D:\\Projects -1\\Catalogs\\Holster Bianchi\\Catalog\\UM_Ready_Sku_Abv50.csv";// Util.getHelperBean().getInputFile(); // read input file path
            String outputFilePath = "D:\\Projects -1\\Catalogs\\Holster Bianchi\\Catalog\\UM_Ready_Sku_Abv50-1.csv";//Util.getHelperBean().getOutputFilePath();// read output file path

            File inputFile = new File(inputFilePath);
            BufferedReader inputScan = null; // Scanner is used to read a whole line or character from the input file 
            java.util.Hashtable<Integer, String> skuIds = new java.util.Hashtable<Integer, String>();

            if (inputFile.exists()) {
                FileInputStream fis = new FileInputStream( inputFile );
                DataInputStream in = new DataInputStream(fis);
                //inputScan = new BufferedReader(new FileReader(inputFile));
                inputScan = new BufferedReader(new InputStreamReader(in));
                StringBuffer skuId = null;
                String line;
                while ((line = inputScan.readLine())!= null) {
                    skuId = new StringBuffer();
                    if (inLineCount > 0) {
                        String[] splitLine = line.split("\"");
                        skuId.append(splitLine[9]);
                        String changedSKU = changeSKU( skuId.toString());
                    }//close if( lineCount>0)
                    inLineCount++;
                }//close while( inputScan.hasNext())		
                inputScan.close();
            }// close if( inputFile.exists())

            System.out.println("Processed the input file ....");

            JOptionPane.showMessageDialog(this,  "File genareted with total lines " + outLineCount + " / " + inLineCount + " lines at "+ outputFilePath, "Done",JOptionPane.INFORMATION_MESSAGE );
    }
    int outLineCount = 0;
    int inLineCount = 0;

    private String changeSKU(String aSKU) throws Exception{
        
        String [] splittedSKU = aSKU.split(",");
        String commonSKU = aSKU.substring(0,aSKU.lastIndexOf("_"));
        int commonSKULen = commonSKU.length();
        
        String modelNumbers = aSKU.substring(aSKU.lastIndexOf("_")+1);
        int modelNumbLen = modelNumbers.length();
        if(commonSKULen>49) {
            System.out.println("commonSKU : " + commonSKU);
            System.out.println("modelNumbers : " + modelNumbers);
        }
//        if(modelNumbLen>commonSKULen && modelNumbLen > 50-commonSKULen) {
//            System.out.println("commonSKULen : " + commonSKULen);
//            System.out.println("modelNumbLen : " + modelNumbLen);
//            System.out.println("commonSKU : " + commonSKU);
//            System.out.println("modelNumbers : " + modelNumbers);
//            outLineCount++;
//        }
        
        return "";
               
    }
}
